package api

import (
	"github.com/gin-gonic/gin"
	"shop/config/constant"
	"shop/dao"
	"shop/service"
	"shop/utils/convertutil"
	"shop/utils/result"
	"shop/utils/sessionutil"
)

// LoginTestUser 测试号登录
func LoginTestUser(c *gin.Context) {
	if c.Query("code") != "123456" {
		result.RenderFail(c, "密码错误")
		return
	}
	user := dao.GetUserByShopIdAndUsername(convertutil.ParseInt(c.GetHeader(constant.ParamHeaderShopId)), c.Query("username"))
	if user == nil || user.Id == 0 {
		result.RenderFail(c, "测试号不存在")
		return
	}
	token := sessionutil.SetUserSession(user)
	result.RenderResult(c, result.Ok(token))
}

// LoginByWeixin 微信登录
func LoginByWeixin(ctx *gin.Context) {
	shopId := convertutil.ParseInt(ctx.GetHeader(constant.ParamHeaderShopId))
	wxCode := ctx.Query("code") // 微信小程序登录时必传
	result.RenderResult(ctx, service.LoginByWeixin(shopId, wxCode))
}

func Logout(ctx *gin.Context) {
	sessionutil.Logout(sessionutil.GetUserIdFromCtx(ctx))
	result.RenderResult(ctx, result.Ok(nil))
}
